import 'package:equatable/equatable.dart';
import 'package:backend.core/core/domain/entities/api/app_store/api_app_store_one_time_purchased_item_transaction.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_time_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_transaction_offer_type_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_transaction_ownership_type_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/app_store/api_app_store_transaction_revocation_info_dto.dart';

class ApiAppStoreOneTimePurchasedItemTransactionDto extends Equatable
    implements ApiAppStoreTransactionDto {
  const ApiAppStoreOneTimePurchasedItemTransactionDto._internal({
    required this.id,
    required this.timeDto,
    required this.offerTypeDto,
    required this.ownershipTypeDto,
    required this.revocationInfoDto,
  });

  @override
  final String id;

  @override
  final ApiTimeDto timeDto;

  @override
  final ApiAppStoreTransactionOfferTypeDto offerTypeDto;

  @override
  final ApiAppStoreTransactionOwnershipTypeDto ownershipTypeDto;

  @override
  final ApiAppStoreTransactionRevocationInfoDto? revocationInfoDto;

  @override
  List<Object?> get props {
    return [
      id,
      timeDto,
      offerTypeDto,
      ownershipTypeDto,
      revocationInfoDto,
    ];
  }

  factory ApiAppStoreOneTimePurchasedItemTransactionDto.fromEntity(
      ApiAppStoreOneTimePurchasedItemTransaction transaction) {
    final timeDto = ApiTimeDto.fromEntity(transaction.time);
    final offerTypeDto = ApiAppStoreTransactionOfferTypeDto.fromEntity(transaction.offerType);
    final ownershipTypeDto =
        ApiAppStoreTransactionOwnershipTypeDto.fromEntity(transaction.ownershipType);

    ApiAppStoreTransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto =
          ApiAppStoreTransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    return ApiAppStoreOneTimePurchasedItemTransactionDto._internal(
      id: transaction.id,
      timeDto: timeDto,
      offerTypeDto: offerTypeDto,
      ownershipTypeDto: ownershipTypeDto,
      revocationInfoDto: revocationInfoDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiAppStoreTransactionIdKey: id,
      kApiAppStoreTransactionTimeKey: timeDto.toString(),
      kApiAppStoreTransactionOfferTypeKey: offerTypeDto.toString(),
      kApiAppStoreTransactionOwnershipTypeKey: ownershipTypeDto.toString(),
      kApiAppStoreTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
    };
  }
}
